package com.xyp.homework;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/8/5
 * @Desc 从想做到能做
 */
class Print {
    public static int a = 1;
    public static int b = 0;
    public static int c = 0;
    public static int count = 1;
    static Object lock = new Object();
}
class PrintA implements Runnable{

    @Override
    public void run() {
        while (true) {
            synchronized (Print.lock) {
                if (Print.count > 100) {
                    break;
                } else {
                    if (Print.a == 0) {
                        try {
                            Print.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        System.out.print("第" + Print.count + "遍：A");
                        Print.a = 0;
                        Print.b = 1;
                        Print.lock.notifyAll();
                    }
                }
            }
        }
    }

}
class PrintB implements Runnable{

    @Override
    public void run() {
        while (true) {
            synchronized (Print.lock) {
                if (Print.count > 100) {
                    break;
                } else {
                    if (Print.b == 0) {
                        try {
                            Print.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        System.out.print("B");
                        Print.b = 0;
                        Print.c = 1;
                        Print.lock.notifyAll();
                    }
                }
            }
        }
    }

}
class PrintC implements Runnable{

    @Override
    public void run() {
        while (true) {
            synchronized (Print.lock) {
                if (Print.count > 100) {
                    break;
                } else {
                    if (Print.c == 0) {
                        try {
                            Print.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        System.out.println("C");
                        Print.c = 0;
                        Print.a = 1;
                        Print.count++;
                        Print.lock.notifyAll();
                    }
                }
            }
        }
    }

}
public class Demo4 {
    public static void main(String[] args) {

        new Thread(new PrintA()).start();
        new Thread(new PrintB()).start();
        new Thread(new PrintC()).start();

    }
}
